package com.briup.ch11;

import java.io.*;

/**
 * @Author briup-adam
 * @Date 2023/10/20 上午9:04
 * @Description
 **/

public class ChangeStream {
    public static void  read(String  src){
        BufferedReader br;
        InputStreamReader isr;
        FileInputStream fis;
        try {
            fis=new FileInputStream(src);
            //在转换的 指定字符编码
            isr=new InputStreamReader(fis,"GBK");
            br=new BufferedReader(isr);

            String line=null;
            while ((line=br.readLine())!=null){
                System.out.println(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                //关流 请自己补充
            }catch (Exception e){

            }
        }

    }

    public static  void   write(String  path) throws Exception {
        PrintWriter pw=null;
        OutputStreamWriter osw=null;
        FileOutputStream fos=null;
        fos=new FileOutputStream(path);
        osw=new OutputStreamWriter(fos);
        pw=new PrintWriter(osw);

        pw.println("晓看天色暮看云 行也思君 坐也思君");
        pw.println("春有百花秋有月 夏有凉风冬有雪  若无闲事挂心头 便是人间好时节");
        pw.println("老夫聊发少年狂  左牵黄右擎苍");
        pw.println("抽刀断水水更流  举杯消愁愁更愁");
        pw.println("大鹏一日同风起  扶摇直上九万里");
        pw.println("桃花依旧笑春风  人面不知何处去");
        pw.flush();

        pw.close();
        osw.close();
        fos.close();

    }

    public  static  void   readAndWrite(String src,String dest) throws Exception {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        InputStreamReader isr=null;
        OutputStreamWriter osw=null;
        BufferedReader br=null;
        PrintWriter pw=null;
        fis=new FileInputStream(src);
        isr=new InputStreamReader(fis,"GBK");
        br=new BufferedReader(isr);
        fos=new FileOutputStream(dest);
        osw=new OutputStreamWriter(fos,"UTF-8");
        pw=new PrintWriter(osw);
        String line=null;
        while ((line=br.readLine())!=null){
            if (br.ready())
                pw.println(line);
            else
                pw.print(line);
        }


        pw.close();
        br.close();
        isr.close();
        osw.close();
        fis.close();
        fos.close();
    }

    public static void main(String[] args) {
//        read("02code/src/背影Dest.txt");
//        try {
//            write("a.txt");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        try {
            readAndWrite("02code/src/背影Dest.txt","a.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
